<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>账号密码登录</title>
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/style/admin.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../assets/layui/layui.js"></script>
    <script src="../assets/js/common.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">
                            账号密码登录
                        </div>
                        <div class="layui-card-body">
                            <div id="step_input_username" style="display: block;">
                                <form class="layui-form" action="">
                                    <input type="hidden" name="id" id="wechat_id" value="">
                                    <input type="hidden" name="status" value="1">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">微信号</label>
                                        <div class="layui-input-inline">
                                            <input type="text" id="username" name="username" lay-verify="username" autocomplete="off" placeholder="请输入微信号" class="layui-input" value="">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">密码</label>
                                        <div class="layui-input-inline">
                                            <input type="password" id="password" name="password" lay-verify="password" autocomplete="off" placeholder="请输入密码" class="layui-input" value="">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <a class="layui-btn layui-btn-radius" lay-submit lay-filter="formSubmit">提交</a>
                                            <a class="layui-btn layui-btn-radius layui-btn-primary" href="index.html">取消</a>
                                        </div>
                                    </div>
                                </form>
                            </div>

                            <div id="step_login_process" style="display: none;">
                                <div class="layui-form-item">
                                    <label class="layui-form-label">微信号</label>
                                    <div class="layui-input-inline">
                                        <label class="layui-form-label" id="login_username"></label>
                                    </div>
                                </div>
                                <div>
                                    <label class="layui-form-label"></label>
                                    <div class="layui-input-inline">
                                        <label id="login_note"><i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop" style="font-size: 30px; color: green;"></i> 登录中...请勿刷新或关闭页面</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        var weChatId;
        var wayId;
        var weChatUsername;
        var seconds = 0;
        var loading = false;
        var keepProcess = true;
        layui.use(['form'], function(){
            var $ = layui.$
                ,form = layui.form;

            var showIframe = function (title, url) {
                layer.open({
                    type: 2,
                    title: title,
                    shadeClose: false,
                    shade: 0.8,
                    area: ['400px', '500px'],
                    content: url,
                    btn: ['已完成验证，下一步'],
                    yes: function (index, layero) {
                        doLogin(weChatId, wayId);
                        loading = true;
                        seconds = 0;
                        setTimeout(function () {checkLogin(wayId);}, 3000);
                        layer.close(index);
                    },
                    cancel: function(index, layero){
                        loading = false;
                        keepProcess = false;
                        layer.close(index);
                        $('#step_input_username').css('display', 'block');
                        $('#step_login_process').css('display', 'none');
                        layer.msg('已取消登录', {icon: 5});
                        return false;
                    }
                });
            };

            var checkLogin = function (wayId) {
                if (seconds >= 60) {
                    loading = false;
                    $('#login_note').html('<i class="layui-icon layui-icon-face-cry" style="font-size: 30px; color: red;"></i> 登录超时，请稍后在试');
                }
                if (loading == false) return;
                $.ajax({
                    type: "GET",
                    url: "/wechat/pwdLoginProcess/" + wayId,
                    success: function (response) {
                        var dataObj = $.parseJSON(response);
                        if (dataObj.code === 0) {
                            if (dataObj.msg && dataObj.url) {
                                loading = false;
                                showIframe(dataObj.msg, dataObj.url);
                            }
                            if (dataObj.login == 1) {
                                loading = false;
                                $('#login_note').html('<i class="layui-icon layui-icon-auz" style="font-size:40px;color: #00CC00;"></i>' + ' 登录成功，正在跳转页面...');
                                keepProcess = false;
                                var targetUrl = '/wechat/index';
                                if (dataObj.wechat_id > 0 && dataObj.is_grouped==false) {
                                    targetUrl = '/wechat/editGroup/'+dataObj.wechat_id;
                                }
                                setTimeout(function () {
                                    location.href = targetUrl;
                                }, 1000);
                            } else if (dataObj.login === 0) {

                            } else if (dataObj.login == -1) {
                                if (dataObj.url == '') {
                                    loading = false;
                                    keepProcess = false;
                                    $('#step_input_username').css('display', 'block');
                                    $('#step_login_process').css('display', 'none');
                                    layer.msg(dataObj.msg, {icon: 5});
                                }
                            } else if (dataObj.login == -2) {
                                loading = false;
                                keepProcess = false;
                                layer.msg(dataObj.msg, {icon: 5});
                                setTimeout(function(){
                                    location.href = '/wechat/index';
                                }, 1000);
                            }
                        } else {
                            keepProcess = false;
                            layer.msg(dataObj.msg, {icon: 5});
                        }
                    },
                    error: function (request, status, error) {
                        keepProcess = false;
                        layer.msg('哦噢，网络开小差了', {icon: 5});
                    }
                });
            };

            var processLogin = function(wayId) {
                console.log(seconds+' seconds, '+loading);
                if (loading) {
                    setTimeout(function () {checkLogin(wayId);}, 3000);
                }
                if (keepProcess) {
                    setTimeout(function () {
                        seconds += 3;
                        processLogin(wayId);
                    }, 3000);
                }
            };

            var doLogin = function (weChatId, wayId) {
                $.ajax({
                    type: "GET",
                    url: "/wechat/newLoginTask/"+weChatId+"/"+wayId,
                    success:function(response){
                        var dataObj = $.parseJSON(response);
                        if(dataObj.code == 0) {
//                        layer.msg(dataObj.msg, {icon: 1});
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }
                    },
                    error: function (request, status, error) {

                    }
                });
            };

            form.verify({
                username: function(value){
                    if(value.length < 1){
                        return '请填写微信号';
                    }
                    weChatUsername = value;
                }
                , password: function (value) {
                    if (value.length < 1) {
                        return '请填写密码';
                    }
                }
            });

            //监听提交
            form.on('submit(formSubmit)', function(data){
                data.field.password = hex_md5(data.field.password);
//            layer.msg('提交中', {
//                shadeClose: false
//                ,icon: 16
//                ,shade: 0.01
//            });
                $.ajax({
                    type: "POST",
                    url: "/wechat/save",
                    data: data.field,
                    success:function(response){
                        var dataObj=$.parseJSON(response);
                        if (dataObj.code===0) {
                            layer.msg('提交成功，等待登录...', {icon: 6});
//                        setTimeout(function(){
//                            location.href = '/wechat/index';
//                        }, 1000);
                        }
                        if (dataObj.wayId && dataObj.wayId>0) {
                            $('#password').val('');
                            weChatId = dataObj.weChatId;
                            wayId = dataObj.wayId;
                            $('#wechat_id').val(weChatId);
                            $('#login_username').html(weChatUsername);
                            $('#step_input_username').css('display', 'none');
                            $('#step_login_process').css('display', 'block');
                            doLogin(weChatId, wayId);
                            loading = true;
                            seconds = 0;
                            setTimeout(processLogin(wayId), 3000);
                        } else {
                            layer.msg(dataObj.msg, {icon: 5});
                        }
                    },
                    error: function (request, status, error) {
                        layer.msg('哦噢，网络开小差了', {icon: 5});
                    }
                });
                return false;
            });
        });
    </script>
</div>
</body>
</html>
